// pages/evaluate/evaluate.js
const app =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filelength:4,
    imgurl:app.globalData.Imageurl,
    filelist:[],
    addshow:true,
    appraiseImag:'',
    fileimg:[],
    appraiseContent:'',
    

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var sjs=this.randomWord()
    var datatime = Date.parse(new Date());
    var tokens=''+sjs+''+datatime+''+app.globalData.userId
    this.setData({
      token:tokens
    })

    console.log(this)
    this.setData({
      goodsOrderItemId:options.goodsOrderItemId
    }) 
  },
  //删除选择
  deleimg(e){
    var shuz=this.data.filelist
    console.log(shuz)
      shuz.splice(e.currentTarget.dataset.deleid,1)
      console.log(shuz)
      if (shuz.length<4) {
        this.setData({
          addshow:true,
        })
      }
      this.setData({
        filelist:shuz
      })
     
    console.log(e)
  },
  //选择图片
  fileup(){
    var t=this
     var countlegth= t.data.filelength-t.data.filelist.length
      
      wx.chooseImage({
        count: countlegth,
        sizeType: ['original', 'compressed'],
       sourceType: ['album', 'camera'],
       success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        console.log(res)
        const tempFilePaths = res.tempFilePaths
        t.setData({
          filelist:t.data.filelist.concat(tempFilePaths)
        })
        if (t.data.filelist.length<4) {
          t.setData({
            addshow:true,
          })
        }else{
          t.setData({
            addshow:false,
          })
        }
        }
      })
      
      
    
    


   
  },
 
saveFileUpload(files){
  wx.request({
    url: app.globalData.Interfaceurl + 'saveFileUpload',
    method: "post",
    header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
    },
    data: {
        userId: app.globalData.userId,
        file:files
    },
    success: res => {
        console.log(res)
       
        
    }
})

},
bindblur(e){
  console.log(e)
  this.setData(
    {
      appraiseContent:e.detail.value
    }
  )
},
bindinput(e){
  console.log(e)
  this.setData(
    {
      appraiseContent:e.detail.value
    }
  )
},
randomWord() {
  var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
   var nums = "";
    for (var i = 0; i < 20; i++) {
    var id = parseInt(Math.random() * 61);
    nums += chars[id]
    }
    return nums; 
    },

//保存评论
saveGoodsAppraise(){

  // let pages=getCurrentPages();
  // let prevPage=pages[pages.length - 3]
  // prevPage.setData({
  //   papenaxtto:1,
  // })
  // wx.navigateBack({
  //   delta: 2,
  // })


  var t=this

  //有图片上传
if (t.data.appraiseContent!='') {
  wx.showLoading({
    title: '正在提交评价',
    mask:true
  })  

if (t.data.filelist.length>0) {
  t.setData({
    max:t.data.filelist.length
  })
  for(let i=0; i<t.data.filelist.length;i++){
    // t.saveFileUpload(t.data.filelist[i])
    
    wx.uploadFile({
        url: app.globalData.Interfaceurl + 'saveFileUpload',
        filePath: t.data.filelist[i],
        name: 'file',
        formData: {
            userId:app.globalData.userId,
        },
        header: { 
            "content-type": "multipart/form-data"
        },
        success (res){
          console.log(res)
            
            console.log(JSON.parse(res.data))
            t.setData({
              fileimg:t.data.fileimg.concat(JSON.parse(res.data).fileUrl)
            })
            
            
            if ((i+1)==t.data.max) {
              var appraiseImagstr =t.data.fileimg.toString()
              console.log(appraiseImagstr)
              wx.request({
                url: app.globalData.Interfaceurl + 'saveGoodsAppraise',
                method: "post",
                header: {
                    "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
                },
                data: {
                    userId: app.globalData.userId,
                    token:t.data.token,
                    goodsOrderItemId:t.data.goodsOrderItemId,
                    appraiseContent:t.data.appraiseContent,
                    appraiseImage:appraiseImagstr,
                },
                success: res => {
                  console.log(res)
                  wx.hideLoading();
                  let pages=getCurrentPages();
                  let prevPage=pages[pages.length - 3]
                  prevPage.setData({
                    papenaxtto:1,
                  })
                  wx.navigateBack({
                    delta: 2,
                  })
                    
                }
              })
              
            }

         
        
        },
        fail (res){
          console.log(res)
        },
      })
  
  
   }
   
   //无图片上传
  }else{
    wx.request({
      url: app.globalData.Interfaceurl + 'saveGoodsAppraise',
      method: "post",
      header: {
          "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
          userId: app.globalData.userId,
          goodsOrderItemId:t.data.goodsOrderItemId,
          appraiseContent:t.data.appraiseContent,
          token:t.data.token,
      },
      success: res => {
         console.log(res)
         
         wx.hideLoading();
         wx.navigateBack({
          delta: 2,
        })
      }
  })


  }
}else{
  wx.showToast({
    title: '请输入您的评价',
    icon:'none'
  })
}

  
  
  
},


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})